﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO.Ports;

namespace CabinetComTest
{
    public partial class MainForm : Form
    {
        public static SerialPort serialPort = new SerialPort();

        public MainForm()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            comboBox1.DataSource = SerialPort.GetPortNames();
            textBox2.Text = "" +
                "J4 风机开关\r\n" +
                "J5 门锁开关\r\n" +
                "J7 灯开关\r\n" +
                "J9 网络预警\r\n" +
                "J20 滤芯预警\r\n" +
                "J21 柜门预警";

            Cabinet.SendCmd_Event += Cabinet_SendCmd_Event;
            Cabinet.GetAsw_Event += Cabinet_GetAsw_Event;
        }

        private void Cabinet_GetAsw_Event(string obj)
        {
            listBox1.Items.Add(obj);
            listBox1.SelectedIndex = listBox1.Items.Count - 1;
        }

        private void Cabinet_SendCmd_Event(string obj)
        {
            listBox1.Items.Add(obj);
            listBox1.SelectedIndex = listBox1.Items.Count - 1;
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (serialPort.IsOpen == false)
            {
                serialPort.PortName = comboBox1.SelectedValue.ToString();
                serialPort.BaudRate = 115200;
                serialPort.DataBits = 8;
                serialPort.Parity = Parity.None;
                serialPort.StopBits = StopBits.One;
                serialPort.NewLine = "\r\n";
                serialPort.ReadTimeout = 200;
                serialPort.WriteTimeout = 200;

                try
                {
                    serialPort.Open();
                }
                catch (Exception excp)
                {
                    MessageBox.Show(excp.Message);
                    return;
                }

                comboBox1.Enabled = false;
                MessageBox.Show("串口打开成功");
            }
            else
            {
                MessageBox.Show("串口已经打开！");
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (serialPort.IsOpen)
            {
                try
                {
                    serialPort.Close();
                }
                catch (Exception excp)
                {
                    MessageBox.Show(excp.Message);
                    return;
                }

                comboBox1.Enabled = true;
                MessageBox.Show("串口关闭成功");
            }
            else
            {
                MessageBox.Show("串口已经关闭！");
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            MessageBox.Show(Cabinet.LightOpen());
        }

        private void button4_Click(object sender, EventArgs e)
        {
            MessageBox.Show(Cabinet.LightClose());
        }

        private void button6_Click(object sender, EventArgs e)
        {
            MessageBox.Show(Cabinet.FanOpen());
        }

        private void button5_Click(object sender, EventArgs e)
        {
            MessageBox.Show(Cabinet.FanClose());
        }

        private void button8_Click(object sender, EventArgs e)
        {
            MessageBox.Show(Cabinet.LockOpen());
        }

        private void button7_Click(object sender, EventArgs e)
        {
            MessageBox.Show(Cabinet.LockClose());
        }

        private void button9_Click(object sender, EventArgs e)
        {
            MessageBox.Show(Cabinet.HandShake());
        }

        private void button10_Click(object sender, EventArgs e)
        {
            MessageBox.Show(Cabinet.FilterLifeSet());
        }

        private void button11_Click(object sender, EventArgs e)
        {
            MessageBox.Show(Cabinet.FilterLifeClear());
        }

        private void button12_Click(object sender, EventArgs e)
        {
            MessageBox.Show(Cabinet.NetworkSet());
        }

        private void button13_Click(object sender, EventArgs e)
        {
            MessageBox.Show(Cabinet.NetworkClear());
        }

        private void button14_Click(object sender, EventArgs e)
        {
            MessageBox.Show(Cabinet.DoorSet());
        }

        private void button15_Click(object sender, EventArgs e)
        {
            MessageBox.Show(Cabinet.DoorClear());
        }

        private void button16_Click(object sender, EventArgs e)
        {
            textBox1.Text = Newtonsoft.Json.JsonConvert.SerializeObject(Cabinet.GetStatus(), Newtonsoft.Json.Formatting.Indented);
        }
    }
}
